package servlet;

import pojo.User;
import dao.UserSql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m=request.getParameter("m");
        //注册用户信息
        if("sign".equals(m)){
            sign(request,response);
        }
        //查询用户信息
        else if("queryUserMsg".equals(m)){
            queryUserMsg(request,response);
        }
        //查询用户信息更改页面
        else if("queryUser".equals(m)){
            queryUser(request,response);
        }
        //更改用户信息不包含密码
        else if("updateUser".equals(m)){
            updateUser(request,response);
        }
        //传递用户信息到修改密码页面
        else if("updatePwd1".equals(m)){
            updatePwd1(request,response);
        }
        //在修改页面修改密码
        else if("updatePwd2".equals(m)){
            updatePwd2(request,response);
        }
    }

    protected void updatePwd2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oldpwd = UserSql.getOldpwd(Integer.parseInt(request.getParameter("id")));
        System.out.println(oldpwd);
        String o_pwd = request.getParameter("o_pwd");
        String n_pwd = request.getParameter("n_pwd");
        String pwd=request.getParameter("pwd");
        if(oldpwd.equals(o_pwd)){
            if(n_pwd.equals(pwd)){
                UserSql.updatePwd(Integer.parseInt(request.getParameter("id")), n_pwd);
                request.setAttribute("msg", "密码修改成功!");
                request.getRequestDispatcher("searchUserMsg.jsp").forward(request, response);
            }else{
                int id=Integer.parseInt(request.getParameter("id"));
                request.setAttribute("id", id);
                request.setAttribute("msg", "两次输入的新密码不一致！");
                request.getRequestDispatcher("updateUserpwd.jsp").forward(request, response);
            }
        }
        else{
            int id=Integer.parseInt(request.getParameter("id"));
            request.setAttribute("id", id);
            request.setAttribute("msg", "原密码错误！");
            request.getRequestDispatcher("updateUserpwd.jsp").forward(request, response);
        }
    }

    protected void updatePwd1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("id", request.getParameter("id"));
        request.getRequestDispatcher("updateUserpwd.jsp").forward(request, response);
    }

    protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User u=new User();
        u.setId(Integer.parseInt(request.getParameter("u_id")));
        System.out.println(Integer.parseInt(request.getParameter("u_id")));
        u.setName(request.getParameter("u_name"));
        u.setPhone(request.getParameter("u_phone"));
        String u_name=request.getParameter("u_name");
        if(UserSql.updateUser(u)==true){
            request.setAttribute("msg", "修改成功");
            request.getRequestDispatcher("searchUserMsg.jsp").forward(request, response);
        }else {
            String msg="修改信息错误";
            response.sendRedirect("UserServlet?m=queryUser&&name"+ URLEncoder.encode(u_name,"UTF-8")+"&&msg="+URLEncoder.encode(msg,"UTF-8"));
        }
    }

    protected void queryUserMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String u_name=request.getParameter("u_name");
        List<User> list = UserSql.getUserName(u_name);
        if(list.size()==0){
            request.setAttribute("msg", "没有此用户");
            request.getRequestDispatcher("searchUserMsg.jsp").forward(request,response);
        }else {
            request.setAttribute("list", list);
            request.getRequestDispatcher("queryUser.jsp").forward(request, response);
        }
    }

    protected void queryUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        List<User> list = UserSql.queryUser(name);
        User user=new User();
        for(int i=0;i<list.size();i++){
            user=(User)list.get(i);
        }
        System.out.println(user);
        request.setAttribute("id", user.getId());
        request.setAttribute("name", user.getName());
        request.setAttribute("pwd", user.getPwd());
        request.setAttribute("phone", user.getPhone());
        request.setAttribute("list",list);
        request.getRequestDispatcher("queryUserMessage.jsp").forward(request, response);
    }


    protected void sign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname=request.getParameter("uname");
        String pwd1=request.getParameter("password2");
        String pwd2=request.getParameter("password2");
        String phone=request.getParameter("phone");
            User user=new User();
            user.setName(uname);
            user.setPhone(phone);
            user.setPwd(pwd1);
            UserSql.save(user);
            request.setAttribute("msg","注册成功");
            request.getRequestDispatcher("login.jsp").forward(request,response);
    }



}
